//
//  MainViewController.swift
//  weibo
//
//  Created by guo3920591 on 16/4/15.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
       //在viewDidLoad中只会添加自控制器，不会创建uitabbarbutton
       //tabbar的按钮会在viewwillappear(视图将要显示之前创建)
        addChildViewControllers()
        
         setupTabbar()
        
        
        showNewFeature()
        
       
        
    }
    
    override func viewWillAppear(animated: Bool) {
         super.viewWillAppear(animated)
        
        //将按钮设置到顶部
        tabBar.bringSubviewToFront(composebutton)
        
        //计算按钮的位置
        
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)-1//-1是为了边界容错处理
        //CGRectInset(rect: CGRect, _ dx: CGFloat, _ dy: CGFloat) -> CGRect
        //dx,dy>0 向内缩小 dx，dy<0 想外扩张
        
        composebutton.frame = CGRectInset(tabBar.bounds, width*2, 0)
        
      
        
        
    }
    
 /// 发布按钮的点击
    //private 这个关键字修饰函数 会使这个函数私有 运行循环会找不到这个函数，如果发送消息会抛出异常
    //@objc 关键字能够在“编译"的时候，让函数变成OC的消息机制发送
 @objc  private  func clickComposeButton(){
        
       print("click点击了")
    
    
    
     let vc = PublishView()
    
    
    
    vc.showPublish(self)
    
    
    
        
    }
    
    // MARK: - 私有属性
    /// 添加发布按钮
    private lazy var composebutton: UIButton = UIButton(
        gxl_title: nil,
        imgName: "tabbar_compose_icon_add",
        backImgName: "tabbar_compose_button")
    
 
    
    
    
    
}
// MARK: - 新特性处理
extension MainViewController{
/// 显示新特性
    private func showNewFeature(){
        //如果没有登录直接返回
        if !OAuthModel.shareUserAccount.isLogin{
            
            return
        }
        
        //判断是否有新版本
        
        let v = isNewVision ? FeatureView() : WelcomeView()
        
     
        //将视图添加到当前视图
        
        view.addSubview(v)
        
    }
/// 是否新版本
    private var isNewVision: Bool{
        
        //获取当前版本
       let currentvision = Vision
        //获取之前的版本
        let visionkey = "cn.itcast.vision"
        
        let oldvision = NSUserDefaults.standardUserDefaults().stringForKey(visionkey)
        //把当前版本存入到沙盒
        NSUserDefaults.standardUserDefaults().setObject(currentvision, forKey: visionkey)
        
        return currentvision != oldvision
    }
    
    private var Vision:String{
        
        guard let vision = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String else{
            
            return "0.0.0"
        }
        
       
        return vision
    }
    
    
}



// MARK: - 设置tabbar
extension MainViewController {
    /**
     设置tabbar
     */
    private func setupTabbar(){
        //要设置tabbar的背景图片，以下两句代码必须连用 nav同理
        //提供图像会自动拉伸
        tabBar.shadowImage = UIImage.gxl_singleDimage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage=UIImage(named: "tabbar_background")
        
        //设置tabbar的背景颜色
      // tabBar.barTintColor = UIColor.redColor()
       //添加发布按钮
        tabBar.addSubview(composebutton)
        
          //给发布按钮添加点击事件
        
        composebutton.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
        
        
        
        
    }
    
    
}

// MARK: - 添加控制器
extension MainViewController {
    
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title: "首页", imgname: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imgname: "tabbar_message_center")
        //添加一个空白的控制器
        addChildViewController(UIViewController())
        addChildViewController(DiscoverViewController(), title: "发现", imgname: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imgname: "tabbar_profile")
        
    }
    
    
    
    private func addChildViewController(vc: UIViewController,title: String,imgname: String) {
        
      
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imgname)
        
        vc.tabBarItem.selectedImage = UIImage(named: imgname + "_selected")
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor(red: 255/255.0, green: 109/255.0, blue: 0.0, alpha: 1.0)], forState: UIControlState.Selected)
        //  设置文本字体大小 注意要使用nomal
        
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: UIControlState.Normal)
        
        
        let nav = UINavigationController(rootViewController: vc)
        
        
        addChildViewController(nav)
        
        
        
    }
    
    
    
}
